Name "SohaServiceSetup"
OutFile "SohaServiceSetup.exe"
RequestExecutionLevel admin

!include LogicLib.nsh
!include "nsProcess.nsh"
!include "Registry.nsh"

InstallDir '$PROGRAMFILES\SohaService'

SetCompress Auto
SetDateSave On
SetDataBlockOptimize On
SetOverwrite on
SilentInstall silent

Section
IfFileExists $WINDIR\SYSWOW64\*.* Is64bit Is32bit
Is32bit:
    SetRegView 32
    GOTO End32Bitvs64BitCheck
 
Is64bit:
    SetRegView 64
 
End32Bitvs64BitCheck:
SimpleSC::StopService "SohaService" "" "30"
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
  ;detailprint $0
${Do}
${nsProcess::FindProcess} "SohaService.exe" $R0
detailprint $R0
${If} $R0 != 0
${Break}
${EndIf}
Sleep 100
SimpleSC::StopService "SohaService" "" "30"
Pop $0
detailprint $0
${Loop}
SetOutPath $INSTDIR
SetOverwrite on
File /r ".\SohaService\*"

SimpleSC::RemoveService "SohaService"
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)
  ;detailprint $0

# SimpleSC::InstallService [name_of_service] [display_name] [service_type] [start_type]
# [binary_path] [dependencies] [account] [password]

SimpleSC::InstallService "SohaService" "SohaService" "16" "2" "$INSTDIR\SohaService.exe" "" "" ""
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
;detailprint $0

${If} $0 <> 0

            #SimpleSC::GetErrorMessage

            #Pop $0

            DetailPrint `Could not install service! - Reason: $0`
            SetErrorLevel 2

        ${EndIf}

# Start service
SimpleSC::StartService "SohaService" "" "30"
Pop $0 ; returns an errorcode (<>0) otherwise success (0)
;detailprint $0
${registry::CreateKey} "HKLM\SOFTWARE\SohaUpdater\Applications\SohaService" $0
;detailprint $0
${registry::Write} "HKLM\SOFTWARE\SohaUpdater\Applications\SohaService" "Name" "SohaService" "REG_SZ" $0
;detailprint $0
${registry::Write} "HKLM\SOFTWARE\SohaUpdater\Applications\SohaService" "Path" "$PROGRAMFILES\SohaService" "REG_SZ" $0
;detailprint $0
${registry::Write} "HKLM\SOFTWARE\SohaUpdater\Applications\SohaService" "Version" "1.2" "REG_SZ" $0
;detailprint $0
${registry::Write} "HKLM\SOFTWARE\SohaUpdater\Applications\SohaService" "FirstRun" "true" "REG_SZ" $0
;detailprint $0
SectionEnd
BrandingText "SohaLauncher"
